/**
 * 工具函数
 */
//封装一个拦截登录的函数
function checkLogin() {
    const token = window.localStorage.getItem("token")
    const user = window.localStorage.getItem("user")
    if (!token && !user) {
        window.location.href = "./login.html"
    }
}

//
function tick(status, msg, fn) {
    let div = document.createElement("div")
    div.classList.add("tickCss")
    div.innerHTML = `
    <i>${status == 0 ? "√" : "X"}</i>
    <p>${msg}</p>
    `
    document.body.append(div)
    setTimeout(function () {
        div.remove()
        fn && fn() //短路或,遇到false就不会往后执行
    }, 2000)
}

//封装一个函数,实现生成页尾结构
function creatFooter(n) {
    let footer = document.createElement("footer")
    footer.innerHTML = `
            <a href='./index.html' class="${n == 0 ? "active" : ""}">
            <i class="iconfont iconhome"></i>
            <span>首页</span>
        </a>
        <a href='./sport.html' class="${n == 1 ? "active" : ""}">
            <i class="iconfont iconsports"></i>
            <span>运动</span>
        </a>
        <a href='./mine.html' class="${n == 2 ? "active" : ""}">
            <i class="iconfont iconmine"></i>
            <span>我们</span>
        </a>
    `
    document.body.append(footer)
}

window.$utils = {
    checkLogin: checkLogin,
    tick: tick,
    creatFooter: creatFooter
}